<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="申请类型" prop="applicationType">
        <el-select v-model="queryParams.applicationType" placeholder="请选择申请类型" clearable>
          <el-option label="迁入企业" value="1" />
          <el-option label="新成立企业" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="企业名称" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入企业名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option label="待审核" value="0" />
          <el-option label="审核通过" value="1" />
          <el-option label="审核不通过" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="applicationList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="申请类型" align="center" prop="applicationType">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.applicationType === '1'" type="success">迁入企业</el-tag>
          <el-tag v-else type="warning">初创企业</el-tag>
        </template>
      </el-table-column>
      enterpriseName legalRepresentative  legalRepresentativePhone
      <el-table-column label="企业名称" align="center" width="180">
        <template #default="scope">
          {{ scope.row.applicationType === '1' ? scope.row.enterpriseName : scope.row.itemName }}
        </template>
      </el-table-column>
      <el-table-column label="法定代表人" align="center" width="180">
        <template #default="scope">
          {{ scope.row.applicationType === '1' ? scope.row.legalRepresentative : scope.row.contactName }}
        </template>
      </el-table-column>
      <el-table-column label="联系电话" align="center" width="180">
        <template #default="scope">
          {{ scope.row.applicationType === '1' ? scope.row.legalRepresentativePhone : scope.row.contactNamePhone }}
        </template>
      </el-table-column>
      <el-table-column label="申请时间" align="center" prop="createTime" width="180" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" type="warning">待审核</el-tag>
          <el-tag v-else-if="scope.row.status === 1" type="success">审核通过</el-tag>
          <el-tag v-else-if="scope.row.status === 2" type="danger">审核不通过</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            >查看</el-button
          >
          <el-button
            v-if="scope.row.status === 0"
            size="mini"
            type="text"
            icon="el-icon-check"
            @click="handleApprove(scope.row)"
            >审核</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" label-width="120px">
        <!-- 企业基本信息 -->
        <el-card>
          <div slot="header" class="header">企业基本信息</div>
          <template v-if="queryParams.applicationType === '1'">
            <el-form-item label="申请时间">{{ form.createTime }}</el-form-item>
            <el-form-item label="企业名称">{{ form.enterpriseName }}</el-form-item>

            <el-form-item label="法定代表人">{{ form.legalRepresentative }}</el-form-item>
            <el-form-item label="联系电话">{{ form.legalRepresentativePhone }}</el-form-item>
            <el-form-item label="邮箱地址">{{ form.legalRepresentativeEmail }}</el-form-item>
            <el-form-item label="经办人">{{ form.handler }}</el-form-item>
            <el-form-item label="经办人电话">{{ form.handlerPhone }}</el-form-item>
            <el-form-item label="经办人邮箱">{{ form.handlerEmail }}</el-form-item>
            <el-form-item label="注册资本">{{ form.registeredCapital }}万元</el-form-item>
            <el-form-item label="实缴资本">{{ form.paidInCapital }}万元</el-form-item>
            <el-form-item label="初始入园人数">{{ form.initialParkEntryCount }}</el-form-item>
            <el-form-item label="预计成立日期">{{ form.establishmentDate }}</el-form-item>
            <el-form-item label="企业性质">{{ form.enterpriseNature }}</el-form-item>
            <el-form-item label="企业类型">{{ form.enterpriseType }}</el-form-item>
            <el-form-item label="所属行业">{{ form.industry }}</el-form-item>
            <el-form-item label="主营业务">{{ form.mainBusiness }}</el-form-item>
          </template>
          <template v-else>
            <el-form-item label="申请时间">{{ form.createTime }}</el-form-item>
            <el-form-item label="企业名称">{{ form.itemName }}</el-form-item>
            <el-form-item label="负责人">{{ form.contactName }}</el-form-item>
            <el-form-item label="联系电话">{{ form.contactPhone }}</el-form-item>
            <el-form-item label="邮箱地址">{{ form.contactEmail }}</el-form-item>
            <el-form-item label="经办人">{{ form.agentName }}</el-form-item>
            <el-form-item label="经办人电话">{{ form.agentPhone }}</el-form-item>
            <el-form-item label="经办人邮箱">{{ form.agentEmail }}</el-form-item>
            <el-form-item label="注册资本">{{ form.registerMoney }}万元</el-form-item>
            <el-form-item label="实际募集资金">{{ form.raisedMoney }}万元</el-form-item>
            <el-form-item label="初始员工人数">{{ form.initialEmployees }}</el-form-item>
            <el-form-item label="预计注册日期">{{ form.expectedRegisterDate }}</el-form-item>
            <el-form-item label="企业性质">{{ form.region }}</el-form-item>
            <el-form-item label="所属行业">{{ form.industry }}</el-form-item>
            <el-form-item label="主营业务">{{ form.mainBusiness }}</el-form-item>
            <el-form-item label="持股比例">{{ form.shareholdingRatio }}</el-form-item>
            <el-form-item label="经营范围">{{ form.businessScope }}</el-form-item>
          </template>
        </el-card>

        <!-- 园区场地租赁需求 -->
        <el-card>
          <div slot="header" class="header">园区场地租赁需求</div>
          <template v-if="queryParams.applicationType === '1'">
            <el-form-item label="租赁面积">{{ form.rentalArea }}平方米</el-form-item>
            <el-form-item label="租期">{{ form.leaseTerm }}年</el-form-item>
            <el-form-item label="位置要求">{{ form.locationRequirement }}</el-form-item>
            <el-form-item label="其他要求">{{ form.otherRequirements }}</el-form-item>
          </template>
          <template v-else>
            <el-form-item label="租赁面积">{{ form.rentalArea }}</el-form-item>
            <el-form-item label="租期">{{ form.rentalPeriod }}</el-form-item>
            <el-form-item label="位置要求">{{ form.locationRequirement }}</el-form-item>
            <el-form-item label="楼层要求">{{ form.floorRequirement }}</el-form-item>
            <el-form-item label="电力要求">{{ form.powerRequirement }}</el-form-item>
            <el-form-item label="用水要求">{{ form.waterRequirement }}</el-form-item>
            <el-form-item label="网络要求">{{ form.networkRequirement }}</el-form-item>
            <el-form-item label="其他要求">{{ form.otherRequirement }}</el-form-item>
          </template>
        </el-card>

        <!-- 股东构成 -->
        <el-card>
          <div slot="header" class="header">股东构成</div>
          <template v-if="queryParams.applicationType === '1'">
            <el-table :data="form.shareholders" style="width: 100%">
              <el-table-column prop="shareholderName" label="股东姓名" />
              <el-table-column prop="shareRatio" label="股份比例" />
              <el-table-column prop="shareholderNature" label="股东性质" />
            </el-table>
          </template>
          <template v-else>
            <el-table :data="form.shareholders" style="width: 100%">
              <el-table-column prop="name" label="股东姓名" />
              <el-table-column prop="ratio" label="股份比例" />
              <el-table-column prop="nature" label="股东性质" />
            </el-table>
          </template>
        </el-card>

        <!-- 主要人员简介 -->
        <el-card>
          <div slot="header" class="header">主要人员简介</div>
          <template v-if="queryParams.applicationType === '1'">
            <el-table :data="form.keyPersonnel" style="width: 100%">
              <el-table-column prop="name" label="姓名" />
              <el-table-column prop="position" label="职务" />
              <el-table-column prop="graduatedSchool" label="毕业院校" />
              <el-table-column prop="title" label="职称" />
              <el-table-column prop="education" label="学历" />
              <el-table-column prop="major" label="专业" />
            </el-table>
          </template>
          <template v-else>
            <el-table :data="form.members" style="width: 100%">
              <el-table-column prop="name" label="姓名" />
              <el-table-column prop="job" label="职务" />
              <el-table-column prop="school" label="毕业院校" />
              <el-table-column prop="title" label="职称" />
              <el-table-column prop="education" label="学历" />
              <el-table-column prop="major" label="专业" />
              <el-table-column prop="birthDate" label="出生年月" />
            </el-table>
          </template>
        </el-card>

        <!-- 项目介绍和分析 -->
        <el-card>
          <div slot="header" class="header">项目介绍和分析</div>
          <template v-if="queryParams.applicationType === '1'">
            <el-form-item label="项目简介">{{ form.projectIntroduction }}</el-form-item>
            <el-form-item label="竞争优势">{{ form.competitiveAdvantageAnalysis }}</el-form-item>
            <el-form-item label="市场前景">{{ form.marketProspectAnalysis }}</el-form-item>
            <el-form-item label="能耗情况">{{ form.energyConsumption }}</el-form-item>
            <el-form-item label="污染情况">{{ form.pollution }}</el-form-item>
            <el-form-item label="噪声情况">{{ form.noise }}</el-form-item>
          </template>
          <template v-else>
            <el-form-item label="项目简介">{{ form.projectIntroduction }}</el-form-item>
            <el-form-item label="竞争优势">{{ form.competitiveAdvantage }}</el-form-item>
            <el-form-item label="市场前景">{{ form.marketProspects }}</el-form-item>
            <el-form-item label="能耗情况">{{ form.energyConsumption }}</el-form-item>
            <el-form-item label="污染情况">{{ form.pollution }}</el-form-item>
            <el-form-item label="噪声情况">{{ form.noise }}</el-form-item>
            <el-form-item label="其他条件">{{ form.otherConditions }}</el-form-item>
          </template>
        </el-card>

        <!-- 知识产权 -->
        <el-card v-if="queryParams.applicationType === '1'">
          <div slot="header" class="header">知识产权</div>
          <el-table :data="form.intellectualProperties" style="width: 100%">
            <el-table-column prop="name" label="名称" />
            <el-table-column prop="type" label="类型" />
          </el-table>
        </el-card>

        <!-- 资助与获奖情况 -->
        <el-card v-if="queryParams.applicationType === '1'">
          <div slot="header" class="header">资助与获奖情况</div>
          <el-table :data="form.fundingAndAwards" style="width: 100%">
            <el-table-column prop="level" label="级别" />
            <el-table-column prop="projectAwardName" label="项目/获奖名称" />
            <el-table-column prop="governmentFundingAmount" label="政府资助金额" />
            <el-table-column prop="awardingDepartmentAndDate" label="授予部门及日期" />
          </el-table>
        </el-card>

        <!-- 审核意见 -->
        <template v-if="queryParams.applicationType === '1'">
          <el-form-item label="审核意见" v-if="form.status === 0">
            <web-editor v-model="form.scienceParkOpinion" :min-height="150" :show-extra-tools="true" />
          </el-form-item>
          <el-form-item label="审核意见" v-else>
            <web-editor v-model="form.scienceParkOpinion" :min-height="150" :read-only="true" :show-extra-tools="false" />
          </el-form-item>
        </template>
        <template v-else>
          <el-form-item label="教师意见" v-if="form.status === 0">
            <el-input
              type="textarea"
              v-model="form.teacherOpinion"
              placeholder="请输入教师意见"
            />
          </el-form-item>
          <el-form-item label="学院意见" v-if="form.status === 0">
            <el-input
              type="textarea"
              v-model="form.collegeOpinion"
              placeholder="请输入学院意见"
            />
          </el-form-item>
          <el-form-item label="办公室意见" v-if="form.status === 0">
            <el-input
              type="textarea"
              v-model="form.officeOpinion"
              placeholder="请输入办公室意见"
            />
          </el-form-item>
          <el-form-item label="领导意见" v-if="form.status === 0">
            <el-input
              type="textarea"
              v-model="form.leaderOpinion"
              placeholder="请输入领导意见"
            />
          </el-form-item>
          <el-form-item label="教师意见" v-else>
            <el-input
              type="textarea"
              v-model="form.teacherOpinion"
              disabled
            />
          </el-form-item>
          <el-form-item label="学院意见" v-else>
            <el-input
              type="textarea"
              v-model="form.collegeOpinion"
              disabled
            />
          </el-form-item>
          <el-form-item label="办公室意见" v-else>
            <el-input
              type="textarea"
              v-model="form.officeOpinion"
              disabled
            />
          </el-form-item>
          <el-form-item label="领导意见" v-else>
            <el-input
              type="textarea"
              v-model="form.leaderOpinion"
              disabled
            />
          </el-form-item>
        </template>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="form.status === 0">
        <el-button type="primary" @click="handleApproveSubmit(1)">审核通过</el-button>
        <el-button type="danger" @click="handleApproveSubmit(2)">审核不通过</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getImmigrantApplicationList,
  getImmigrantApplicationDetail,
  editImmigrantApplication,
  delImmigrantApplication,
  getEnterpriseApplicationList,
  getEnterpriseApplicationDetail,
  editEnterpriseApplication,
  delEnterpriseApplication
} from "@/api/system/enter-application";
import {sourceMap} from "@vue/cli-service/lib/config/terserOptions";

export default {
  name: "EnterApplication",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 申请表列表
      applicationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        current: 1,
        pageSize: 10,
        applicationType: null,
        itemName: null,
        status: null
      },
      // 表单参数
      form: {
        opinion: ""
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询申请表列表 */
    getList() {

      this.loading = true;
      if (this.queryParams.applicationType === "1") {
        // 对于迁入企业，使用enterpriseName作为搜索参数
        const params = {...this.queryParams};
        if (params.itemName) {
          params.enterpriseName = params.itemName;
          delete params.itemName;
        }
        getImmigrantApplicationList(params).then(response => {
          this.applicationList = response.data.records.map(item => ({ ...item, applicationType: "1" }));
          this.total = response.data.total;
          this.loading = false;
        });
      } else if (this.queryParams.applicationType === "2") {
        // 对于新成立企业，将enterpriseName映射为itemName
        const params = {...this.queryParams};
        getEnterpriseApplicationList(params).then(response => {
          this.applicationList = response.data.records.map(item => ({ ...item, applicationType: "2" }));
          this.total = response.data.total;
          this.loading = false;
        });
      } else {
        // 如果没有选择类型，获取所有类型的申请
        console.log(this.queryParams)
        // 为不同类型的API准备不同的参数
        const immigrantParams = {...this.queryParams};
        const enterpriseParams = {...this.queryParams};

        // 如果有企业名称搜索，为企业应用准备itemName参数

        // 为了正确处理分页，我们需要获取更多数据然后在前端进行分页处理
        // 或者建议用户选择具体的申请类型进行查询
        const maxPageSize = 1000; // 设置一个较大的页面大小来获取所有数据
        immigrantParams.size = maxPageSize;
        enterpriseParams.size = maxPageSize;
        immigrantParams.current = 1;
        enterpriseParams.current = 1;

        Promise.all([
          getImmigrantApplicationList(immigrantParams),
          getEnterpriseApplicationList(enterpriseParams)
        ]).then(([immigrantRes, enterpriseRes]) => {
          // 合并所有数据
          const allData = [
            ...immigrantRes.data.records.map(item => ({ ...item, applicationType: "1" })),
            ...enterpriseRes.data.records.map(item => ({ ...item, applicationType: "2" }))
          ];

          // 按创建时间排序（最新的在前）
          allData.sort((a, b) => new Date(b.createTime) - new Date(a.createTime));

          // 计算分页
          const startIndex = (this.queryParams.current - 1) * this.queryParams.pageSize;
          const endIndex = startIndex + this.queryParams.pageSize;

          this.applicationList = allData.slice(startIndex, endIndex);
          this.total = allData.length;
          this.loading = false;
        });
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        opinion: ""
      };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.reset();
      const id = row.id;
      // 设置当前查看的申请类型
      this.queryParams.applicationType = row.applicationType;

      if (row.applicationType === "1") {
        getImmigrantApplicationDetail(id).then(response => {
          this.form = response.data;
          console.log(this.form)
          this.open = true;
          this.title = "查看迁入企业申请";
        });
      } else {
        getEnterpriseApplicationDetail(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "查看新成立企业申请";
        });
      }
    },
    /** 审核按钮操作 */
    handleApprove(row) {
      this.handleView(row);
    },
    /** 提交审核 */
    handleApproveSubmit(status) {
      const data = {
        ...this.form,
        status: status
      };
      if (this.queryParams.applicationType === "1") {
        console.log(data);
        editImmigrantApplication(data).then(response => {
          this.$modal.msgSuccess("审核成功");
          this.open = false;
          this.queryParams.applicationType=null;
          this.getList();
        });
      } else {
        editEnterpriseApplication(data).then(response => {
          console.log(data);
          this.$modal.msgSuccess("审核成功");
          this.open = false;
          this.queryParams.applicationType=null;
          this.getList();
        });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除该申请？').then(() => {
        if (row.applicationType === "1") {
          return delImmigrantApplication(id);
        } else {
          return delEnterpriseApplication(id);
        }
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>

<style scoped>
.header {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 20px;
}
.el-card {
  margin-bottom: 20px;
}
</style>
